from app.outdoor.models import ActivityType
from app.aoduo_admin.models import AdminOperationLog
from django.http import JsonResponse
from rest_framework.views import APIView
from common.dictInfo import model_to_dict
from common.authCheck.adminAuth import getAdminUser, check_admin_login, adminAuthCheck
from common.paginator import paginator


class AdminActivityTypeListInfoView(APIView):

    @check_admin_login
    def get(self, request):
        """
        获取活动类型列表
        :param request:
        :return:
        """
        try:
            admin_user = getAdminUser(request.session.get('admin'))
            authID = 11
            if adminAuthCheck(admin_user, authID):
                # 记录操作行为
                AdminOperationLog.objects.create(
                    related_admin=admin_user,
                    related_auth_type_id=authID,
                    other_message='view activity type list'
                )
                allActivityType = ActivityType.objects.all()
                pageActivityType = paginator(allActivityType, request.GET.get('page'), 50)
                allActivityTypeDict = [model_to_dict(activityType) for activityType in pageActivityType]
                return JsonResponse({
                    'status': True,
                    'all_activity_type': allActivityTypeDict,
                    'has_previous': pageActivityType.has_previous(),
                    'has_next': pageActivityType.has_next()
                })
            return JsonResponse({
                'status': False,
                'errMsg': '你没有权限操作'
            }, status=401)
        except Exception as ex:
            return JsonResponse({
                'status': False,
                'errMsg': str(ex)
            }, status=403)
